﻿#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.IO;
/// <summary>
/// SharpTemplete模板解析器
/// </summary>
public class ESF_CSharpTempleteParser : AbsESF_TempleteParser
{
    /// <summary>
    /// 解析代码模板
    /// </summary>
    /// <param name="_codeTemplete">代码模板</param>
    /// <returns>代码</returns>
    protected override List<T> OnResolver<T>(string _codeTemplete)
    {
        List<T> result = new List<T>();
        List<AbsESF_WriteBlock> blocks = ESF_CSharpCompilerTool.ResolveWriteBlock(_codeTemplete);
        StringWriter writer = new StringWriter();
        foreach (AbsESF_WriteBlock key in blocks)
        {
            key.Resolver();
            switch (key.classify)
            {
                case enESF_CompilerWriteBlockClassify.TextBlock:
                    writer.Write(string.Format("writer.Write({0});{1}", key.GetCode().EncodeToJson(), Environment.NewLine));
                    break;
                case enESF_CompilerWriteBlockClassify.CodeBlock:
                    #region 语法规则
                    switch (key.syntax)
                    {
                        case enESF_CompilerWriteBlockSyntax.EqualSignSssignment:                            
                            writer.Write(string.Format("writer.Write({0});{1}", key.GetCode(), Environment.NewLine));
                            break;
                        case enESF_CompilerWriteBlockSyntax.ExecuteCode:
                            writer.Write(key.GetCode());
                            break;
                        case enESF_CompilerWriteBlockSyntax.InlineFunction:                            
                            result.Add((T)Activator.CreateInstance(typeof(T), key.GetCode(), enESF_CodeResult.InlineFunction));
                            break;
                        case enESF_CompilerWriteBlockSyntax.Using:
                            result.Add((T)Activator.CreateInstance(typeof(T), key.GetCode(), enESF_CodeResult.Using));
                            break;
                    }
                    #endregion
                    break;
            }
        }
        result.Add((T)Activator.CreateInstance(typeof(T), writer.ToString(), enESF_CodeResult.CodeBlock));
        return result;
    }
}
#endif
